<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\model\PayTrade;
use apps\marriage\core\payment\WxPay;
use apps\marriage\core\service\CourseOrderService;
use apps\marriage\core\service\PayTradeService;
use apps\marriage\core\service\ServiceChargeOrderService;
use apps\marriage\core\service\ServicePackageOrderService;
use apps\marriage\core\service\SiteConfigService;
use apps\marriage\core\service\UserPlatformService;
use apps\marriage\core\service\VipLevelOrderService;
use think\Exception;
use think\Request;

class WxPayController extends BaseMarriageMpController
{
    /**
     * @var SiteConfigService
     */
    private $siteConfigService;

    /**
     * @var UserPlatformService
     */
    private $userPlatformService;

    /**
     * @var PayTradeService
     */
    private $payTradeService;

    /**
     * @var VipLevelOrderService
     */
    private $vipLevelOrderService;

    /**
     * @var ServiceChargeOrderService
     */
    private $serviceChargeOrderService;

    /**
     * @var CourseOrderService
     */
    private $courseOrderService;

    /**
     * @var ServicePackageOrderService
     */
    private $servicePackageOrderService;

    protected function init()
    {
        parent::init();
        $this->siteConfigService = service('SiteConfig', SERVICE_NAMESPACE);
        $this->userPlatformService = service('UserPlatform', SERVICE_NAMESPACE);
        $this->payTradeService = service('PayTrade', SERVICE_NAMESPACE);
        $this->vipLevelOrderService = service('VipLevelOrder', SERVICE_NAMESPACE);
        $this->serviceChargeOrderService = service('ServiceChargeOrder', SERVICE_NAMESPACE);
        $this->courseOrderService = service('CourseOrder', SERVICE_NAMESPACE);
        $this->servicePackageOrderService = service('ServicePackageOrder', SERVICE_NAMESPACE);
    }

    public function query(Request $request)
    {
        $tradeNo = $request->param('pay_trade_no');
        $tradeResult = $this->payTradeService->query($tradeNo);
        return $this->success([
            'success' => $tradeResult ? 1 : 0
        ]);
    }

    public function create_vip_level_pay_trade(Request $request)
    {
        $orderSn = $request->param('order_sn');
        $order = $this->vipLevelOrderService->getByOrderSn($orderSn);
        if(!$order || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $appId = $this->siteConfigService->getValueByCode('weixin_mp_app_id');
        $userPlatform = $this->userPlatformService->info([
            'appid' => $appId,
            'user_id' => $this->user['id']
        ]);
        $payConfig = [
            'app_id' => $appId,
            'app_secret' => $this->siteConfigService->getValueByCode('weixin_mp_app_secret'),
            'mch_id' => $this->siteConfigService->getValueByCode('weixin_mp_mch_id'),
            'pay_key' => $this->siteConfigService->getValueByCode('weixin_mp_pay_sign_key'),
        ];
        $payment = new WxPay($payConfig);

        $data = [
            'order_id' => $order['id'],
            'order_sn' => $order['order_sn'],
            'order_type' => PayTrade::ORDER_TYPE_VIP_LEVEL,
            'order_amount' => $order['order_money'],
            'payment_code' => 'WxPay',
            'payment_name' => $payment->getName(),
            'appid' => $appId
        ];
        $tradeNo = $this->payTradeService->create($data);

        $body = '订购服务';
        $jsParams = $payment->buildPreOrder($tradeNo, $order['order_money'], 'JSAPI', $userPlatform['open_id'], $body);
        return $this->success([
            'pay_params' => $jsParams,
            'pay_trade_no' => $tradeNo
        ]);
    }

    public function service_charge_order(Request $request)
    {
        $orderSn = $request->param('order_sn');
        $order = $this->serviceChargeOrderService->getByOrderSn($orderSn);
        if(!$order || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $appId = $this->siteConfigService->getValueByCode('weixin_mp_app_id');
        $userPlatform = $this->userPlatformService->info([
            'appid' => $appId,
            'user_id' => $this->user['id']
        ]);
        $payConfig = [
            'app_id' => $appId,
            'app_secret' => $this->siteConfigService->getValueByCode('weixin_mp_app_secret'),
            'mch_id' => $this->siteConfigService->getValueByCode('weixin_mp_mch_id'),
            'pay_key' => $this->siteConfigService->getValueByCode('weixin_mp_pay_sign_key'),
        ];
        $payment = new WxPay($payConfig);

        $data = [
            'order_id' => $order['id'],
            'order_sn' => $order['order_sn'],
            'order_type' => PayTrade::ORDER_TYPE_SERVICE_CHARGE,
            'order_amount' => $order['order_money'],
            'payment_code' => 'WxPay',
            'payment_name' => $payment->getName(),
            'appid' => $appId
        ];
        $tradeNo = $this->payTradeService->create($data);

        $body = '订购服务';
        $jsParams = $payment->buildPreOrder($tradeNo, $order['order_money'], 'JSAPI', $userPlatform['open_id'], $body);
        return $this->success([
            'pay_params' => $jsParams,
            'pay_trade_no' => $tradeNo
        ]);
    }

    public function create_course_order_pay_trade(Request $request)
    {
        $orderSn = $request->param('order_sn');
        $order = $this->courseOrderService->getBySn($orderSn);
        if(!$order || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $appId = $this->siteConfigService->getValueByCode('weixin_mp_app_id');
        $userPlatform = $this->userPlatformService->info([
            'appid' => $appId,
            'user_id' => $this->user['id']
        ]);
        $payConfig = [
            'app_id' => $appId,
            'app_secret' => $this->siteConfigService->getValueByCode('weixin_mp_app_secret'),
            'mch_id' => $this->siteConfigService->getValueByCode('weixin_mp_mch_id'),
            'pay_key' => $this->siteConfigService->getValueByCode('weixin_mp_pay_sign_key'),
        ];
        $payment = new WxPay($payConfig);

        $data = [
            'order_id' => $order['id'],
            'order_sn' => $order['sn'],
            'order_type' => PayTrade::ORDER_TYPE_COURSE,
            'order_amount' => $order['pay_money'],
            'payment_code' => 'WxPay',
            'payment_name' => $payment->getName(),
            'appid' => $appId
        ];
        $tradeNo = $this->payTradeService->create($data);

        $body = '订购服务';
        $jsParams = $payment->buildPreOrder($tradeNo, $order['pay_money'], 'JSAPI', $userPlatform['open_id'], $body);
        return $this->success([
            'pay_params' => $jsParams,
            'pay_trade_no' => $tradeNo
        ]);
    }

    public function create_service_package_order_pay_trade(Request $request)
    {
        $orderSn = $request->param('order_sn');
        $order = $this->servicePackageOrderService->getByOrderSn($orderSn);
        if(!$order || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $appId = $this->siteConfigService->getValueByCode('weixin_mp_app_id');
        $userPlatform = $this->userPlatformService->info([
            'appid' => $appId,
            'user_id' => $this->user['id']
        ]);
        $payConfig = [
            'app_id' => $appId,
            'app_secret' => $this->siteConfigService->getValueByCode('weixin_mp_app_secret'),
            'mch_id' => $this->siteConfigService->getValueByCode('weixin_mp_mch_id'),
            'pay_key' => $this->siteConfigService->getValueByCode('weixin_mp_pay_sign_key'),
        ];
        $payment = new WxPay($payConfig);

        $data = [
            'order_id' => $order['id'],
            'order_sn' => $order['order_sn'],
            'order_type' => PayTrade::ORDER_TYPE_SERVICE_PACKAGE,
            'order_amount' => $order['order_money'],
            'payment_code' => 'WxPay',
            'payment_name' => $payment->getName(),
            'appid' => $appId
        ];
        $tradeNo = $this->payTradeService->create($data);

        $body = '订购服务';
        $jsParams = $payment->buildPreOrder($tradeNo, $order['order_money'], 'JSAPI', $userPlatform['open_id'], $body);
        return $this->success([
            'pay_params' => $jsParams,
            'pay_trade_no' => $tradeNo
        ]);
    }
}